package org.opens.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * mybatis扫描包路径配置, 分页器配置
 *
 * @author zym
 * @Date 2022/6/28 14:18
 */
@Configuration
@MapperScan({"org.opens"})
public class MybatisConfig {

    /**
     * mybatis-plus3.4.1的分页插件配置方式, 旧版本配置方式：
     * ```
     * @Bean(name = "paginationInterceptor")
     * public PaginationInterceptor paginationInterceptor() {
     *     PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
     *     // paginationInterceptor.setLimit(你的最大单页限制数量，默认 500 条，小于 0 如 -1 不受限制);
     *     return paginationInterceptor;
     * }
     * ```
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        // 设置数据库类型
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        // 设置每页最大行数
        paginationInnerInterceptor.setMaxLimit(500L);
        // 加入插件管理器
        interceptor.addInnerInterceptor(paginationInnerInterceptor);

        return interceptor;
    }

}
